
<template>
  <div class="kpi-container">
    <!-- 时间范围选择 -->
    <div class="time-range">
      <el-radio-group v-model="timeRange" size="large">
        <el-radio-button label="day">今日</el-radio-button>
        <el-radio-button label="week">本周</el-radio-button>
        <el-radio-button label="month">本月</el-radio-button>
        <el-radio-button label="quarter">本季度</el-radio-button>
        <el-radio-button label="custom">自定义</el-radio-button>
      </el-radio-group>
      
      <el-date-picker
        v-model="customDateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :disabled="timeRange !== 'custom'"
        class="ml-4"
      />
    </div>

    <!-- KPI总览卡片 -->
    <el-row :gutter="20" class="kpi-overview">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>运输准时率</span>
              <el-tag 
                :type="getStatusType(kpiData.onTimeDelivery.status)"
                size="small"
              >
                {{ kpiData.onTimeDelivery.status }}
              </el-tag>
            </div>
          </template>
          <div class="kpi-value">
            <span class="number">{{ kpiData.onTimeDelivery.value }}%</span>
            <span 
              class="trend" 
              :class="kpiData.onTimeDelivery.trend >= 0 ? 'up' : 'down'"
            >
              {{ Math.abs(kpiData.onTimeDelivery.trend) }}%
              <el-icon>
                <component :is="kpiData.onTimeDelivery.trend >= 0 ? 'ArrowUp' : 'ArrowDown'" />
              </el-icon>
            </span>
          </div>
          <el-progress 
            :percentage="kpiData.onTimeDelivery.value"
            :status="getProgressStatus(kpiData.onTimeDelivery.value)"
          />
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>车辆利用率</span>
              <el-tag 
                :type="getStatusType(kpiData.vehicleUtilization.status)"
                size="small"
              >
                {{ kpiData.vehicleUtilization.status }}
              </el-tag>
            </div>
          </template>
          <div class="kpi-value">
            <span class="number">{{ kpiData.vehicleUtilization.value }}%</span>
            <span 
              class="trend" 
              :class="kpiData.vehicleUtilization.trend >= 0 ? 'up' : 'down'"
            >
              {{ Math.abs(kpiData.vehicleUtilization.trend) }}%
              <el-icon>
                <component :is="kpiData.vehicleUtilization.trend >= 0 ? 'ArrowUp' : 'ArrowDown'" />
              </el-icon>
            </span>
          </div>
          <el-progress 
            :percentage="kpiData.vehicleUtilization.value"
            :status="getProgressStatus(kpiData.vehicleUtilization.value)"
          />
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>客户满意度</span>
              <el-tag 
                :type="getStatusType(kpiData.customerSatisfaction.status)"
                size="small"
              >
                {{ kpiData.customerSatisfaction.status }}
              </el-tag>
            </div>
          </template>
          <div class="kpi-value">
            <span class="number">{{ kpiData.customerSatisfaction.value }}%</span>
            <span 
              class="trend" 
              :class="kpiData.customerSatisfaction.trend >= 0 ? 'up' : 'down'"
            >
              {{ Math.abs(kpiData.customerSatisfaction.trend) }}%
              <el-icon>
                <component :is="kpiData.customerSatisfaction.trend >= 0 ? 'ArrowUp' : 'ArrowDown'" />
              </el-icon>
            </span>
          </div>
          <el-progress 
            :percentage="kpiData.customerSatisfaction.value"
            :status="getProgressStatus(kpiData.customerSatisfaction.value)"
          />
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>成本控制率</span>
              <el-tag 
                :type="getStatusType(kpiData.costControl.status)"
                size="small"
              >
                {{ kpiData.costControl.status }}
              </el-tag>
            </div>
          </template>
          <div class="kpi-value">
            <span class="number">{{ kpiData.costControl.value }}%</span>
            <span 
              class="trend" 
              :class="kpiData.costControl.trend >= 0 ? 'up' : 'down'"
            >
              {{ Math.abs(kpiData.costControl.trend) }}%
              <el-icon>
                <component :is="kpiData.costControl.trend >= 0 ? 'ArrowUp' : 'ArrowDown'" />
              </el-icon>
            </span>
          </div>
          <el-progress 
            :percentage="kpiData.costControl.value"
            :status="getProgressStatus(kpiData.costControl.value)"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- KPI详细分析 -->
    <el-row :gutter="20" class="kpi-details">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>KPI趋势分析</span>
              <el-select v-model="selectedKpi" size="small" style="width: 120px">
                <el-option label="运输准时率" value="onTimeDelivery" />
                <el-option label="车辆利用率" value="vehicleUtilization" />
                <el-option label="客户满意度" value="customerSatisfaction" />
                <el-option label="成本控制率" value="costControl" />
              </el-select>
            </div>
          </template>
          <div class="chart-container">
            <!-- 这里集成实际的图表组件 -->
            <el-empty description="图表加载中...">
              <template #image>
                <el-icon style="font-size: 40px"><TrendCharts /></el-icon>
              </template>
            </el-empty>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>区域KPI对比</span>
              <el-select v-model="selectedRegions" multiple size="small" style="width: 200px">
                <el-option label="华东区" value="east" />
                <el-option label="华北区" value="north" />
                <el-option label="华南区" value="south" />
                <el-option label="西部区" value="west" />
              </el-select>
            </div>
          </template>
          <div class="chart-container">
            <!-- 这里集成实际的图表组件 -->
            <el-empty description="图表加载中...">
              <template #image>
                <el-icon style="font-size: 40px"><DataAnalysis /></el-icon>
              </template>
            </el-empty>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- KPI异常分析 -->
    <el-card class="kpi-alerts">
      <template #header>
        <div class="card-header">
          <span>KPI异常分析</span>
          <el-button-group>
            <el-button type="primary" link @click="exportAlerts">
              导出分析
            </el-button>
            <el-button type="success" link @click="createAlert">
              创建预警
            </el-button>
          </el-button-group>
        </div>
      </template>

      <el-table :data="kpiAlerts" style="width: 100%">
        <el-table-column prop="time" label="时间" width="160" />
        <el-table-column prop="kpi" label="KPI指标" width="120" />
        <el-table-column prop="type" label="异常类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getAlertType(row.type)">
              {{ row.type }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="异常描述" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-button 
                type="primary" 
                link
                @click="handleAlert(row)"
              >
                处理
              </el-button>
              <el-button 
                type="info" 
                link
                @click="viewAlertDetail(row)"
              >
                详情
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {
  TrendCharts,
  DataAnalysis,
  ArrowUp,
  ArrowDown
} from '@element-plus/icons-vue'

// 时间范围
const timeRange = ref('month')
const customDateRange = ref([])

// KPI数据
const kpiData = ref({
  onTimeDelivery: {
    value: 95,
    trend: 2.5,
    status: '良好'
  },
  vehicleUtilization: {
    value: 85,
    trend: -1.2,
    status: '正常'
  },
  customerSatisfaction: {
    value: 92,
    trend: 1.8,
    status: '良好'
  },
  costControl: {
    value: 88,
    trend: -0.5,
    status: '正常'
  }
})

// 图表选项
const selectedKpi = ref('onTimeDelivery')
const selectedRegions = ref(['east', 'north'])

// KPI异常数据
const kpiAlerts = ref([
  {
    time: '2025-03-14 15:30:00',
    kpi: '运输准时率',
    type: '下降预警',
    description: '华东区运输准时率低于目标值5%',
    status: '待处理'
  },
  {
    time: '2025-03-14 14:20:00',
    kpi: '成本控制率',
    type: '异常波动',
    description: '西部区运输成本突增15%',
    status: '处理中'
  }
])

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    '良好': 'success',
    '正常': 'info',
    '警告': 'warning',
    '异常': 'danger',
    '待处理': 'warning',
    '处理中': 'info',
    '已处理': 'success'
  }
  return typeMap[status] || 'info'
}

// 获取进度条状态
const getProgressStatus = (value) => {
  if (value >= 90) return 'success'
  if (value >= 80) return 'warning'
  return 'exception'
}

// 获取预警类型
const getAlertType = (type) => {
  const typeMap = {
    '下降预警': 'warning',
    '异常波动': 'danger',
    '目标预警': 'info'
  }
  return typeMap[type] || 'info'
}

// 导出预警分析
const exportAlerts = () => {
  ElMessage.success('预警分析报告导出中...')
}

// 创建预警规则
const createAlert = () => {
  ElMessage.info('打开预警规则配置')
}

// 处理预警
const handleAlert = (alert) => {
  ElMessage.success(`开始处理预警：${alert.description}`)
}

// 查看预警详情
const viewAlertDetail = (alert) => {
  ElMessage.info(`查看预警详情：${alert.kpi}`)
}
</script>

<style scoped>
.kpi-container {
  padding: 20px;
}

.time-range {
  margin-bottom: 20px;
}

.kpi-overview {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.kpi-value {
  display: flex;
  align-items: baseline;
  margin-bottom: 12px;
}

.kpi-value .number {
  font-size: 24px;
  font-weight: bold;
  color: var(--el-text-color-primary);
}

.kpi-value .trend {
  margin-left: 8px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.trend.up {
  color: #67C23A;
}

.trend.down {
  color: #F56C6C;
}

.chart-container {
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.kpi-details {
  margin-bottom: 20px;
}

.ml-4 {
  margin-left: 16px;
}

:deep(.el-card__header) {
  padding: 12px 20px;
}

:deep(.el-progress-bar__inner) {
  transition: width 0.6s ease;
}
</style>